//
//  NotificaView.m
//  markmap
//
//  Created by 甘世清 on 2024/4/14.
//

#import "NotificaView.h"

@implementation NotificaView

- (void)SQ_UIViewForSelfView{
    self.backgroundColor = UIColor.clearColor;
    UIView * bgView = [[UIView alloc]init];
    bgView.backgroundColor = SQHexColor(@"#FFFFFF", 0.2);
    bgView.layer.cornerRadius = 6;
    bgView.layer.masksToBounds = YES;
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(0, 12, 0, 12));
    }];
    UILabel * topLabel = [[UILabel alloc]init];
    topLabel.text = SQNSLocalString(@"开启通知权限");
    topLabel.textColor = SQColorF1F1F1;
    topLabel.font = FONT_MEDIUM(14);
    [bgView addSubview:topLabel];
    [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView).offset(12);
        make.top.equalTo(bgView).offset(12);
    }];
    UILabel * topLabel2 = [[UILabel alloc]init];
    topLabel2.text = SQNSLocalString(@"有系统消息时, 立刻通知您!");
    topLabel2.textColor = SQColorF1F1F1;
    topLabel2.font = FONT_REGULAR(12);
    [bgView addSubview:topLabel2];
    [topLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView).offset(12);
        make.bottom.equalTo(bgView.mas_bottom).offset(-12);
    }];
    
    UIButton * kai = [UIButton buttonWithType:UIButtonTypeCustom];
    [kai setTitle:SQNSLocalString(@"开通") forState:normal];
    kai.titleLabel.font = FONT_REGULAR(14);
    [kai addTarget:self action:@selector(SQ_setup) forControlEvents:UIControlEventTouchUpInside];
    [kai setTitleColor:SQColor333333 forState:normal];
    kai.backgroundColor = UIColor.whiteColor;
    kai.layer.cornerRadius = 15;
    kai.layer.masksToBounds = YES;
    [bgView addSubview:kai];
    [kai mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(30);
            make.centerY.equalTo(bgView);
            make.right.equalTo(bgView.mas_right).offset(-12);
    }];
}

-(void)SQ_show{
    WS(weakSelf);
    [Manager SQ_checkNotificationAuth:^(BOOL granted) {
        dispatch_async(dispatch_get_main_queue(), ^{
        if (granted) {
                weakSelf.hidden=  YES;
                [weakSelf  mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(0);
                }];
        } else {
                weakSelf.hidden=  NO;
                [weakSelf mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(70);
                }];
        }
        [weakSelf.superview layoutIfNeeded];
        });
    }];
    
}


-(void)SQ_setup{
 
    NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
        }];
    }
}
@end
